package com.bingo.rbac.util;


import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.map.ser.impl.SimpleFilterProvider;
import org.codehaus.jackson.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * json工具类
 *
 * @author bingo
 * @create 2017-12-03 11:16
 **/

public class JsonMapper {

    public static ObjectMapper objectMapper = new ObjectMapper();
     private  static Logger logger = LoggerFactory.getLogger(JsonMapper.class);
    static {
        // config
        objectMapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
        objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
        objectMapper.setFilters(new SimpleFilterProvider().setFailOnUnknownId(false));
        objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_EMPTY);
    }

     public static <T> String objToString(T src){
        if(src==null){
            return null;
        }
        try {

                  return src instanceof String ? (String) src : objectMapper.writeValueAsString(src);
              }catch (Exception e){
                   logger.info(" objToString error");
                   return null;
              }
    }

    public static <T> T StringToObj (String src, TypeReference<T> typeReference){
        if(src!=null){
            return null;
        }
        try {
            return (T) (typeReference.getType().equals(String.class) ? src : objectMapper.readValue(src, typeReference));
        }catch (Exception e){
            logger.info("parse String to Object exception, String:{}, TypeReference<T>:{}, error:{}", src, typeReference.getType(), e);
            return null;
        }
    }
}



